import { listItem } from "@/api/payfee/item";
import { listProject} from "@/api/payfee/project";
import { digitUppercase } from "@/utils/index.js"

export const detailMixin = {
  data(){
    return {
      //收费项目字典
      itemOptions:[],
      //收费类型字典
      projectOptions:[],
      summaryColumn:['normNames','receivableMoney','amountOwed','haveMoney','specialMoney','amountAmount'],
    }
  },
  created() {
    this.getDicts("invoice_status").then(response => {
      this.statusOptions = response.data;
    });
    listItem().then(res=>{
      this.itemOptions = res.rows;
    })
    listProject().then(res=>{
      this.projectOptions = res.rows;
    })
  },
  methods: {
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.invoiceStatus);
    },
    // 收费项目字典翻译
    itemFormat(row, column) {
      let index = this.itemOptions.findIndex(x=>x.chargeId == row.chargeId);
      if(index != -1){
        return this.itemOptions[index].chargeName
      }
      return '';
    },
    // 收费类型字典翻译
    projectFormat(row, column) {
      let index = this.projectOptions.findIndex(x=>x.projectId == row.projectId);
      if(index != -1){
        return this.projectOptions[index].projectType
      }
      return '';
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        let sindex = this.summaryColumn.findIndex(v=>v==column.property);
        if(sindex != -1){
          const values = [];
          this.receivableList.forEach((row) => {
            let index = this.multipleSelection.findIndex(x=>row.id==x.id)
            if(index != -1){
              values.push(Number(row[column.property]) )
            }
          })

          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] = sums[index] > 0? this.$utils.toFixed(sums[index],2):0
            // sums[index] += ' 元';
          } else {
            sums[index] = '0';
          }

        }
      });
      if(sums[6]){
        sums[2]= digitUppercase(sums[6].replace(/ 元/,''))
      }
      return sums;
    },
    // 合计  合并行--单元格合并
    setColSpan() {
      var that = this;
      setTimeout(function () {
        if (that.$refs.table.$el.querySelector(".el-table__footer-wrapper")) {
          var current = that.$refs.table.$el
            .querySelector(".el-table__footer-wrapper")
            .querySelector(".has-gutter");
          var cell = current.rows[0].cells;
          cell[0].colSpan = "2";
          cell[1].style.display = "none";
          cell[2].colSpan = "2";
          cell[3].style.display = "none";
        }
      }, 50);
    },
    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if(row.projectId == this.feeConstants.advance_receipt_type.ysxm){
        if (columnIndex === 1) {
          return [1, 4];
        } else if (columnIndex === 2 || columnIndex === 3 || columnIndex === 4) {
          return [0, 0];
        }
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      //初始化参数
      this.form.preferentialId = null
      this.form.discount = null
      this.form.oddChangeMoney = 0
      this.form.transferAmount = 0
      this.form.obtainMoney = 0
      this.form.receivableMoney = 0
      this.form.specialMoney = 0
      this.showFavourableMoney = false
      this.receivableList.forEach((row) => {
        if( row.ruleCostList &&  row.ruleCostList.length > 0){
          row.ruleCostList.forEach((column) => {
            column.paidMoney = 0
            column.specialMoney = 0
            column.preferentialId = null
          })
        }
      })
      if(val.length > 0){
        this.receivableList.forEach((row) => {
          if(row.receivableMoney == 0){
            this.$refs.table.toggleRowSelection(row, false)
          }
        })
        this.receivableList.forEach((row) => {
          let index = this.multipleSelection.findIndex(x=>x.id == row.id);
          if(index != -1){
            if( row.ruleCostList &&  row.ruleCostList.length > 0){
              row.ruleCostList.forEach((column) => {
                //实收金额===欠费金额
                column.paidMoney = column.amountOwed
              })
            }
          }
        })
      }
      this.computeReceivableList();
    },
    selectCheckbox(selection, row){
      if(row.receivableMoney == 0){
        this.msgError(`收费项目没有欠费条目,请选择有应收金额的费用项目进行缴费。`)
        this.$refs.table.toggleRowSelection(row, false)
      }
    },
    //计算费用
    computeReceivableList(){
        if(this.receivableList.length > 0){
          let haveMoney = 0;
          let specialMoney = 0;
          this.receivableList.forEach((row) => {
            let index = this.multipleSelection.findIndex(x=>row.id==x.id)
            //检查是否选中
            if(index != -1){
              //实收金额
              let detail_haveMoney = 0;
              //预收金额
              if(row.projectId == this.feeConstants.advance_receipt_type.ysxm){
                detail_haveMoney = row.amountOwed
              }else{
                if(row.ruleCostList && row.ruleCostList.length > 0){
                  row.ruleCostList.forEach((column) => {
                    detail_haveMoney = this.$utils.add(detail_haveMoney,column.paidMoney)
                  })
                }
              }
              row.haveMoney = detail_haveMoney;
              //应收金额-时候=优惠金额
              if(detail_haveMoney > 0){
                row.specialMoney = this.$utils.subtract(row.amountOwed,detail_haveMoney)
              }
              specialMoney = this.$utils.add(specialMoney,row.specialMoney)
              haveMoney = this.$utils.add(haveMoney,detail_haveMoney)
            }else{
              row.haveMoney = 0;
              row.specialMoney = 0;
            }
          })
          this.form.billMoney = haveMoney>0?haveMoney:0
          this.form.obtainMoney = haveMoney>0?haveMoney:0
          this.specialMoney = specialMoney
          this.oddChangeMoney = 0
          this.transferAmount = 0

        }
    },
    //收款单对象转义
    receiptFactory(){
      let receiptDetails = [];
      this.receivableList.forEach((row, index) => {
        let index1 = this.multipleSelection.findIndex(x=>x.id == row.id);
        if(index1 != -1){
          receiptDetails.push({
            roomId: row.roomId,
            ownerId: row.ownerId,
            projectId: row.projectId,
            chargeId: row.chargeId,
            normId: row.normId,
            beginTime: row.beginTime,
            finishTime: row.finishTime,
            prices: row.prices,
            receivableMoney: row.receivableMoney,//应收金额
            billMoney: row.haveMoney,//实收金额
            amountAmount:row.amountAmount,//冲抵金额
            specialMoney:row.specialMoney,//优惠金额
            paymentCycle: row.paymentCycle,//缴费周期
            ruleCostIds:row.ruleCostIds,
            ruleCosts:row.ruleCostList
          })
        }
      })
      return{
        roomId: this.pageParam.roomIds,//房屋ID
        receivableMoney: this.form.receivableMoney,//应收金额
        billMoney: this.form.billMoney,//实收金额
        oddChangeMoney: this.form.oddChangeMoney,//找零金额
        transferAmount: this.form.transferAmount,//转存金额
        specialMoney: this.form.specialMoney, //优惠金额
        amountAmount: this.form.amountAmount, //冲抵金额
        preferentialId:this.form.preferentialId,//优惠政策
        discount:this.form.discount, //优惠折扣
        modePayments:this.form.modePayments, //收款方式
        receiptDetails:receiptDetails
      }
    }
  }

}
